package com.syncomponents.remoting;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

import com.syncomponents.core.RequestException;
import com.syncomponents.remoting.message.SoapMessageRequest;


/**
 * soap客户端，需要实现getSoapBean(),返回soap客户端对象
 * 
 * @author sunkey
 * @date Mar 12, 2014
 */
public abstract class AbstractSoapClient extends AbstractClient<SoapMessageRequest> {
    
    protected static final Logger LOGGER = LoggerFactory.getLogger(AbstractSoapClient.class);
    
    
    @Override
    public Object doExcute(SoapMessageRequest messageRequest) {
        Object soapBean = getSoapBean();
        Assert.notNull(soapBean);
        LOGGER.info("soap service name:{}, method:{}",soapBean.getClass().getName(), messageRequest.getMethod());
        
        try {
            Serializable obj = (Serializable)messageRequest.getContent();
            Method method = soapBean.getClass().getMethod(messageRequest.getMethod(), obj.getClass());
            return method.invoke(soapBean, obj);
            
        } catch (NoSuchMethodException e) {
            throw new RequestException(e.getMessage(), e);
        } catch (SecurityException e) {
            throw new RequestException(e.getMessage(), e);
        } catch (IllegalAccessException e) {
            throw new RequestException(e.getMessage(), e);
        } catch (IllegalArgumentException e) {
            throw new RequestException(e.getMessage(), e);
        } catch (InvocationTargetException e) {
            throw new RequestException(e.getMessage(), e);
        }
    }
    
    public abstract Object getSoapBean();

}
